import logging
from django.conf import settings
from common.permission import db
from common.merchant.merchant_db import get_merchant
from common.cp.cp_key_mapper import get_key_mapper as cp_get_key_mapper
from common.dwc.dwc_key_mapper import get_key_mapper as dwc_get_key_mapper

_LOGGER = logging.getLogger(__name__)


def _route_key_mapper_func(merchant_id):
    assert merchant_id in settings.CP_INDEX + settings.DWC_INDEX
    if merchant_id in settings.CP_INDEX:
        return cp_get_key_mapper
    elif merchant_id in settings.DWC_INDEX:
        return dwc_get_key_mapper


def get_permission_options(merchant_id):
    return db.get_merchant_permission_options(merchant_id)


def get_user_menu_list(merchant_id, merchant_role, is_root=False):
    merchant_permission = merchant_role['perm']
    return dict(base_event=db.get_base_event_list(merchant_id, merchant_permission, is_root),
                others=db.get_biz_menu_list(merchant_id, merchant_permission, is_root))


def get_key_mapper(merchant_name, logical_page_id, role_id=None, **kwargs):
    """
    :param merchant_name: 商戶名
    :param logical_page_id: 邏輯頁面代碼
    :param role_id: 用戶角色
    :param kwargs: 額外參數
    :return:
    """
    func = _route_key_mapper_func(merchant_name)
    key_mapper = func(logical_page_id, merchant_name, **kwargs)
    merchant = get_merchant(dict(merchant_name=merchant_name))
    merchant_id = merchant.id
    disable_field_list = db.get_role_page_block_field(merchant_id, role_id, logical_page_id)
    if not disable_field_list:
        return key_mapper
    filtered_key_mapper = []
    for item in key_mapper:
        if item['key'] not in disable_field_list:
            filtered_key_mapper.append(item)
    return filtered_key_mapper


def process_resp_items(resp_items, key_mapper, force_adjust=None):
    """
    用以過濾不可見字段
    :param resp_items: 報表數組
    :param key_mapper: 可見(key, value pair)數組
    :param force_adjust
    :return:
    """
    adjust_amount = settings.PLATFORM_AMOUNT_ADJUST_RATE
    visible_key = {item['key']: item for item in key_mapper}
    visible_report_result = []
    for item in resp_items:
        visible_item = {}
        for field, value in item.items():
            if field in visible_key.keys():
                if visible_key[field].get('adjust_amount', False) or force_adjust:
                    visible_item[field] = value * adjust_amount
                else:
                    visible_item[field] = value
        for field in visible_key.keys():
            if visible_key[field].get('default', False) and visible_item.get(field) in (None, ''):
                visible_item[field] = visible_key[field].get('default')
            copy_from_key = visible_key[field].get('copy_from_if_zero', None)
            copy_from_value = visible_item.get(copy_from_key)
            if copy_from_key and visible_item[field] == 0 and copy_from_value:
                visible_item[field] = copy_from_value
        visible_report_result.append(visible_item)
    return visible_report_result


def create_page_block_field(role_id, perm):
    for perm_id, block_key_list in perm.items():
        db.create_page_block_field(role_id, perm_id, block_key_list)
